import configparser
import json
from threading import Thread
import logging

from logs import setLogger
from mqtt import mqtt_client

from alarm_handle import alarm2_handle,alarm1_handle
class Main:
    def __init__(self):
        conf = configparser.ConfigParser()
        conf.read('./config.ini', encoding="utf-8")
        setLogger(conf)

        self.mqtt_client = mqtt_client
        Thread(target=self.mqtt_init).start()
        Thread(target=self.recv_msg_handler).start()
        logging.info("Starting...")

    def mqtt_init(self):
        self.mqtt_client.connect_and_loop()

    def recv_msg_handler(self):
        while True:
            msg = self.mqtt_client.get_received_messages()
            topic:str = msg.topic
            # logging.info("from topic:%s", topic)
            msg = msg.payload.decode()
            if topic.endswith("alarm1"):
                alarm1_handle(json.loads(msg))
            elif topic.endswith("alarm2"):
                alarm2_handle(json.loads(msg))

            # if msg:
            #     logging.info(("recv_msg_handler", json.loads(msg)))


if __name__ == '__main__':
    Main()

